<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>老师修改</title>

    <link rel="stylesheet" type="text/css" href="/static/js/layui-v2.5.7/layui/css/layui.css">
    <script type="text/javascript" src="/static/js/layui-v2.5.7/layui/layui.js"></script>
    <script type="text/javascript" src="/static/js/myJs.js"></script>

</head>
<body>
<a href="SelectTeacher" class="layui-btn layui-btn-radius layui-btn-primary">
    <i class="layui-icon">&#xe65a;</i>
</a>
<div class="layui-container">
    <div class="layui-row">
        <form action="UpdateTeacher" method="post" class="layui-form">


            <div class="layui-form-item">

                <div class="layui-input-inline">
                    <input type="hidden" name="id" value="{$SelectGet.id}" id="id" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" for="name_id">老师名字</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="name_id" class="layui-input"
                           placeholder="请输入老师名字" required>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" for="password_id">老师密码</label>
                <div class="layui-input-inline">
                    <input type="text" name="password" id="password_id"
                           class="layui-input"
                           placeholder="请输入老师密码" required>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" for="age_id">老师年龄</label>
                <div class="layui-input-inline">
                    <input type="number" name="age" id="age_id" class="layui-input"
                           placeholder="请输入老师年龄" required>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">老师性别</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="男" class="layui-input" title="男">
                    <input type="radio" name="sex" value="女" class="layui-input" title="女">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">科目</label>
                <div class="layui-input-block">

                    <div id="check">

                    </div>

                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" for="create_time_id">时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="create_time" id="create_time_id"
                           class="layui-input"
                           placeholder="请输入时间" required>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" onclick="UPTeacher();" class="layui-btn">提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>

        </form>
    </div>
</div>


<script>

    $(function () {
        layui.use(['form', 'laydate'], function () {
            var form = layui.form;
            window.laydate = layui.laydate;
            Course(form);
            $.ajax({
                dataType: "json",
                type: "GET",
                url: "/studentLayer/Teacher/UpdateTeacher",
                data: {"id": $("#id").val()},
                success: function (ajax_Teacher) {

                    datetime(ajax_Teacher);


                    $("#name_id").val(ajax_Teacher.ajaxdata.name);
                    $("#password_id").val(ajax_Teacher.ajaxdata.password);
                    $("#age_id").val(ajax_Teacher.ajaxdata.age);
                    $("input[name=sex]").each(function (index, element) {
                        if (this.value == ajax_Teacher.ajaxdata.sex) {
                            this.checked = true;
                        }
                        //console.log(this.value);
                    })
                    // console.log(ajax_Teacher.ajaxdata.create_time);

                    $.each(ajax_Teacher.ajaxdata.c_id, function (index, value) {
                        $("input[name='c_name[" + value + "]']").attr('checked', true);
                    })

                    form.render('radio');
                    form.render("checkbox");

                }
            })


        })
    })


    function datetime(ajax_Teacher) {
        window.laydate.render({
            elem: '#create_time_id',
            type: 'datetime',
            value: ajax_Teacher.ajaxdata.create_time,
            theme: '#393D49',
            calendar: true
        })
    }


    function Course(form) {

        $.ajax({
            dataType: "json",
            type: "POST",
            url: "/studentLayer/Student/course",
            success: function (ajax) {
                var html = "";
                ajax.data.forEach(function (value, index) {
                    html = html
                        + '<input type="checkbox" ' +
                        'name="c_name[' + value.id + ']" value="' + value.id + '" title="' + value.name + '" ' +
                        'class="layui-input" >'
                })
                $("#check").html(html);
                form.render("checkbox");
            }


        })
    }

    function UPTeacher() {
        var c_name = new Array();
        $("#check>input:checked").each(function (value, element) {
            c_name.push(element.value);
        })

        $.ajax({
            dataType: "json",
            type: "POST",
            url: "/studentLayer/Teacher/UpdateTeacher",
            data: {
                "id": $("#id").val(),
                "name": $("#name_id").val(),
                "password": $("#password_id").val(),
                "sex": $("input[name=sex]:checked").val(),
                "age": $("#age_id").val(),
                "c_name": c_name,
                "create_time": $("#create_time_id").val()
            },
            success: function (ajax) {
                if (ajax.code == 0) {
                    layer.msg(ajax.msg, {time: 1000, icon: 6}, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.location.reload(index);
                        parent.layer.close(index);

                        window.location.href = "/studentLayer/Teacher/SelectTeacher";
                    })
                } else {
                    layer.msg(ajax.msg, {time: 1000, icon: 5}, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.location.reload(index);
                        parent.layer.close(index);
                        window.location.href = "/studentLayer/Teacher/SelectTeacher";
                    })
                }
            }
        })

    }


</script>


</body>
</html>